<template>
  <div class="base-container">
    <t-form ref="form" :data="queryParams" label-width="80"
      @submit="() => { this.pagination.current = 1; this.queryParams.pageNum = 1; this.getList() }">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="资料名称" name="materialsName">
            <t-input v-model="queryParams.materialsName" clearable placeholder="请输入资料名称" style="width: 240px" />
          </t-form-item>
          <t-form-item label="外部链接" name="materialsUrl">
            <t-input v-model="queryParams.materialsUrl" clearable placeholder="请输入外部链接" style="width: 240px" />
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit"> 查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <div class="btn-group-inline">
      <t-button @click="
        dialog.currentRow = null;
      dialog.open_add_learning_materials_dialog = true;
      " variant="outline" theme="primary">
        <t-icon name="add" slot="icon" />
        新增
      </t-button>
    </div>
    <t-table rowKey="id" :data="data" :columns="columns" hover :pagination="pagination" :loading="dataLoading"
      :key="itemKey">

      <template #materialsFormat="{ row }">
        {{ row.materialsFormat | filterByDict(dict.type.materials_format) }}
      </template>

      <template #files="{ row }">
        <t-link v-for="file in row.fileLists" theme="primary" hover="color" :key="file.id" :href="file.url"
          target="_blank">{{ file.name }}</t-link>
      </template>

      <template #op="{ row }">
        <t-button size="small" @click="
          dialog.currentRow = row;
        dialog.open_add_learning_materials_dialog = true;
        " variant="text" class="t-button-link">
          <t-icon name="edit-1" slot="icon" />
          编辑
        </t-button>
        <t-button @click="
          dialog.currentRow = row;
        dialog.open_learning_detail_dialog = true;
        " size="small" variant="text" class="t-button-link">
          <t-icon name="edit-1" slot="icon" />
          详情
        </t-button>
        <t-button @click="handleDel(row)" size="small" theme="danger" variant="text" class="t-button-link">
          <t-icon name="delete" slot="icon" />
          删除
        </t-button>
      </template>
    </t-table>

    <add-edit-learning-materials-dialog @reload="getList()" :header="dialog.header" :row="dialog.currentRow"
      :obj-id="dialog.objId" :visible.sync="dialog.open_add_learning_materials_dialog"
      v-if="dialog.open_add_learning_materials_dialog"></add-edit-learning-materials-dialog>

    <add-edit-learning-materials-detail @reload="getList()" :header="dialog.header" :row="dialog.currentRow"
      :obj-id="dialog.objId" :visible.sync="dialog.open_learning_detail_dialog"
      v-if="dialog.open_learning_detail_dialog"></add-edit-learning-materials-detail>

  </div>
</template>

<script>
import AddEditLearningMaterialsDialog from './components/add-edit-learning-materials-dialog.vue';
import AddEditLearningMaterialsDetail from './components/add-edit-learning-materials-detail.vue';
import { getFiles } from "@/api/common";

export default {
  name: 'approve-my',
  dicts: ['approve_status', 'sf_project_status', 'sfgc_approve_type', 'materials_format'],
  components: {
    AddEditLearningMaterialsDialog,
    AddEditLearningMaterialsDetail,
  },
  data () {
    return {
      itemKey: '',
      dialog: {
        open: false,
        header: '',
        row: {},
        data: {},
        open_project_detail: false,
        open_add_learning_materials_dialog: false,
        open_outline_detail: false,
        open_learning_detail_dialog: false,
        open_contract_detail: false,
        objId: 0,
        open_approve_list: false,
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialsName: '',
        materialsUrl: '',
      },
      data: [],
      columns: [
        {
          colKey: 'materialsName',
          title: '资料名称',
          align: 'center',
          // ellipsis: true,
          width: 220,
        },
        {
          colKey: 'materialsFormat',
          title: '资料格式',
          align: 'center',
        },
        {
          colKey: 'materialsUrl',
          title: '外部链接',
          align: 'center',
        },
        {
          colKey: 'files',
          title: '附件',
          align: 'center',
        },
        {
          colKey: 'op',
          title: '操作',
          align: 'center',
          width: 200,
        },
      ],

      dataLoading: false,
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination, pageInfo);
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList();
        },
      },
    };
  },
  watch: {
    data: {
      handler (val) {
        val.forEach(row => {
          getFiles(row.id).then((res) => {
            const files = []
            res.data.forEach((item) => {
              files.push({
                name: item.fileName,
                url: item.fullFilePath,
                id: item.id,
                filePath: item.filePath
              })
            })
            row.fileLists = files

            this.itemKey = Math.random()

            this.$forceUpdate()
          }).catch((e) => {
            this.$message.error(e.toString());
          });
        })
      },
      immediate: true
    }
  },
  async created () {
    this.getList();
  },
  methods: {
    getList () {
      this.dataLoading = true;
      this.$api.zjk.learning
        .learningMaterials(this.queryParams)
        .then((response) => {
          this.pagination.total = response.total;
          this.data = response.rows;
          this.dataLoading = false;
        })
        .catch((e) => {
          this.dataLoading = false;
          this.$message.error(e.toString());
        });
    },
    handleDel (row) {
      const confirmDia = this.$dialog.confirm({
        header: '系统提示',
        body: `是否确认删除资料名称为"${row.materialsName}"的数据项？`,
        theme: 'warning',
        onConfirm: ({ e }) => {
          confirmDia.update({
            confirmBtn: {
              loading: true,
            },
          });
          this.$api.zjk.learning
            .deleteLearningMaterials(row.id)
            .then((res) => {
              this.getList();
              this.$message.success('删除成功');
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            })
            .catch((e) => {
              confirmDia.update({
                confirmBtn: {
                  loading: false,
                },
              });
              this.$message.error(e.toString());
            });
        },
      });
    },
  },
};
</script>

<style scoped></style>
